package com.example.memo2.ui.calendar

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.memo2.R
import com.example.memo2.databinding.ItemCalendarDayBinding
import java.text.SimpleDateFormat
import java.util.*

class CalendarAdapter(
    private val onDateSelected: (Date) -> Unit
) : ListAdapter<Date, CalendarAdapter.CalendarViewHolder>(CalendarDiffCallback()) {

    private var selectedDate: Date? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CalendarViewHolder {
        val binding = ItemCalendarDayBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return CalendarViewHolder(binding)
    }

    override fun onBindViewHolder(holder: CalendarViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    fun setSelectedDate(date: Date) {
        val previousSelected = selectedDate
        selectedDate = date
        
        // 更新UI
        if (previousSelected != null) {
            notifyItemChanged(currentList.indexOf(previousSelected))
        }
        notifyItemChanged(currentList.indexOf(date))
    }

    inner class CalendarViewHolder(private val binding: ItemCalendarDayBinding) : 
        RecyclerView.ViewHolder(binding.root) {

        fun bind(date: Date) {
            val calendar = Calendar.getInstance().apply { time = date }
            val today = Calendar.getInstance()
            
            // 设置日期文本
            binding.dayText.text = calendar.get(Calendar.DAY_OF_MONTH).toString()
            
            // 设置星期文本
            binding.weekText.text = SimpleDateFormat("E", Locale.getDefault()).format(date)
            
            // 设置选中状态
            val isSelected = selectedDate?.let { 
                Calendar.getInstance().apply { time = it }.get(Calendar.DAY_OF_YEAR) == 
                calendar.get(Calendar.DAY_OF_YEAR) &&
                Calendar.getInstance().apply { time = it }.get(Calendar.YEAR) == 
                calendar.get(Calendar.YEAR)
            } ?: false
            
            // 是否是今天
            val isToday = calendar.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR) &&
                    calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR)
            
            // 设置背景和文字颜色
            if (isSelected) {
                binding.root.setBackgroundResource(R.drawable.calendar_day_selected_background)
                binding.dayText.setTextColor(ContextCompat.getColor(binding.root.context, android.R.color.white))
                binding.weekText.setTextColor(ContextCompat.getColor(binding.root.context, android.R.color.white))
            } else if (isToday) {
                binding.root.setBackgroundResource(R.drawable.calendar_day_today_background)
                binding.dayText.setTextColor(ContextCompat.getColor(binding.root.context, R.color.primary_color))
                binding.weekText.setTextColor(ContextCompat.getColor(binding.root.context, R.color.primary_color))
            } else {
                binding.root.setBackgroundResource(R.drawable.calendar_day_normal_background)
                binding.dayText.setTextColor(ContextCompat.getColor(binding.root.context, R.color.text_primary))
                binding.weekText.setTextColor(ContextCompat.getColor(binding.root.context, R.color.text_secondary))
            }
            
            // 设置点击事件
            binding.root.setOnClickListener {
                onDateSelected(date)
            }
        }
    }
}

class CalendarDiffCallback : DiffUtil.ItemCallback<Date>() {
    override fun areItemsTheSame(oldItem: Date, newItem: Date): Boolean {
        return oldItem == newItem
    }

    override fun areContentsTheSame(oldItem: Date, newItem: Date): Boolean {
        return oldItem == newItem
    }
} 